﻿#include "Framework/Application.h"
#include "Async/Process.h"
#include "Path/Paths.h"
int main(int argc, char* argv[])
{  
   auto App=NewObject<RApplication>();

   SApplicationArgument Argument;
   Argument.ExecutablePath=argv[0];
   App->Initialize(Argument);

   auto Process=NewObject<RProcess>(App);

   Process->OnStandardOutput.BindLambda([Process]()
   {
      auto Output=Process->ReadAllStandardOutput();
      Output.Add('\0'); //末尾的\0需要自己添加

      //输出到控制台，收到的数据存在粘包问题，需要自己处理
      std::cout<<"Accepted:"<<(const char*)Output.Data();
      std::flush(std::cout);
   });

   Process->OnStandardError.BindLambda([Process]()
   {
      auto Output=Process->ReadAllStandardError();
      Output.Add('\0'); //末尾的\0需要自己添加

      //输出到控制台，收到的数据存在粘包问题，需要自己处理
      std::cerr<<"Error:"<<(const char*)Output.Data();
      std::flush(std::cerr);
   });

   Process->OnFinished.BindLambda([Process](int64_t ExitCode,bool bNormalExit)
   {
      std::cout<<"Process finished, ExitCode:"<<ExitCode<<",NormalExit:"<<bNormalExit<<std::endl;
   });


   //auto TestProgramPath= SPaths::GetExecutablePath().Parent()/U"TestProgram.exe";
   auto TestProgramPath= SPaths::GetExecutablePath().Parent()/U"TestWindowProgram.exe";
   Process->Start(TestProgramPath.ToString() ,TVector<CString>(),true,true);
   // Process->Stop();
   // Process->WaitForFinished(-1);

   std::cout<<"Process ID:"<<Process->GetProcessId()<<std::endl;


   App->Run();

}